using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
//using UnityEngine.Windows;

public abstract class PlayerBasic : MonoBehaviour
{
    // Start is called before the first frame update
    public float health;
    public float healthcurrent;
    public float dietime;
    public bool movable;

    float RepelDistance;
    
    int RepelTimesex = 0, RepelDir;
    public float freeze = 1;
    public float stifftimeex;
    private Animator animator;
    private Rigidbody2D myRigidbody;
    private BoxCollider2D myfeet;
    AnimatorStateInfo stateinfo;
    private bool isground;
    private int jumpnum;
    private bool isjump;
    // private Vector2 tempvel;
    public int face = 1;
    public float Jumpspeed;

    public float Runspeed;
    public float basicrunspeed;
    public int maxjumpnum;
    PlayerAttack playerAttack;
    public int attackable;
    public float healthrecoveryratio;
    public float damagereduction;
    public bool wingyong110;
    GameObject fly,ballet;
    // public IEnumerator Stiff()
    //{

    //myRigidbody.Sleep();

    //    yield return new WaitForSeconds(stifftime);
    // myRigidbody.WakeUp();
    //   freeze = 1;
    //   animator.SetBool("stiff", false);
    //}
    void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Air"))
        {
            myRigidbody.velocity = Vector2.zero;
            Vector3 temp = transform.position;
            temp.x += face * 0.5f;
            transform.position = temp;
        }
    }
    public void Recovery(float healthrecovery)
    {
        healthcurrent += healthrecovery;
        if (healthcurrent > health) healthcurrent = health;
        HealthBar.HealthCurrent = (int)healthcurrent;
    }
    void isdie()
    {
        if (healthcurrent <= 0)
        {
            HealthBar.HealthCurrent = 0;
            animator.SetBool("stiff", false);

            stifftimeex = 99999;
            animator.SetTrigger("die");
            Invoke("Killplayer", dietime);
        }
    }
    public void TakeDamage(float Damage, int face, int repeltimes, float repeldistance, float stifftime)
    {
        if (attackable<0) return;
        freeze = 0;
        if (wingyong110)
        {
            RepelTimesex = 0;
            healthcurrent -= 2*(1 - damagereduction) * Damage;
            stifftimeex = 0;
            RepelDistance = 0;
        }
        else
        {
            RepelTimesex = repeltimes;
            RepelDir = face;
            healthcurrent -= (1- damagereduction)*Damage;
            stifftimeex = stifftime;
            RepelDistance = repeldistance;
        }
       

        Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
        myRigidbody.velocity = vel;
        if (healthcurrent <= 0)
        {
            HealthBar.HealthCurrent = 0;
            animator.SetBool("stiff", false);

            stifftimeex = 99999;
            //animator.SetTrigger("die");
            //Invoke("Killplayer", dietime);
        }
        else {
            HealthBar.HealthCurrent = (int)healthcurrent;
            animator.SetBool("stiff", true); 
          
        }
        //StartCoroutine(Stiff());
        
    }
    void Killplayer()
    {
        if (Time.timeScale > 0)
        {
            Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
            Time.timeScale = 0f;
            //TestGameRoot.Instance.scenesystem.scenestate.panelmanager.Push(new LossPanel());
            GameRoot.Instance.scenesystem.scenestate.panelmanager.Push(new LossPanel());
            //Destroy(gameObject);
        }
        
    }
    public void Start()
    {
        animator = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();
        myfeet = GetComponent<BoxCollider2D>();
        transform.localRotation = Quaternion.Euler(0, 180, 0);

        playerAttack = GetComponentInChildren<PlayerAttack>();
        playerAttack.face = -1;

        health = PlayerData.Health * PlayerData.healthratio;
        Runspeed = PlayerData.Speed * PlayerData.speedratioatio;
        healthcurrent = health;
        HealthBar.HealthMax = (int)health;
        HealthBar.HealthCurrent =(int) health;
        healthrecoveryratio = 0;
        Runspeed = basicrunspeed;
        maxjumpnum = PlayerData.jumpnum;

        ballet = transform.GetChild(1).gameObject;
        ballet.SetActive(false);
        if (PlayerData.ballet)
        {
            
            ballet.SetActive(true);
        }

        fly = transform.GetChild(2).gameObject;
        fly.SetActive(false);
        if (PlayerData.fly)
        {
            myRigidbody.gravityScale = 0;
            fly.SetActive(true);
        }
    }
    void Flip()
    {
        float horizontal = Input.GetAxis("Horizontal");
        if (horizontal < 0)
        {
            transform.localRotation = Quaternion.Euler(0, 0, 0);
            face = 1;
            playerAttack.face = 1;
        }
        else if (horizontal > 0)
        {
            transform.localRotation = Quaternion.Euler(0, 180, 0);
            face = -1;
            playerAttack.face = -1;
        }
    }
    void Move()
    {
        float horizontal = Input.GetAxis("Horizontal");
        //Vector3 dir = new Vector3(horizontal, 0, 0);
        //Vector3 vel = new Vector3(horizontal * Runspeed,0, 0);
        if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A))
        {

            if (isjump == false)
            {
                animator.SetTrigger("move");
                animator.SetBool("move1", false);
                Vector2 vel = new Vector2(0, myRigidbody.velocity.y);

                myRigidbody.velocity = vel;
            }

        }
        
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
        {
            if (isjump == false) animator.SetBool("move1", true);
            //transform.position += vel*Time.deltaTime;
            //transform.rotation = Quaternion.LookRotation(dir);
            Vector2 vel = new Vector2(horizontal * Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else
        {
            Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }


    }
    // Update is called once per frame
    //void Attack()
    //{
     //   if (Input.GetKeyDown(KeyCode.J))
     //   {
      //      animator.SetTrigger("attack");
            //Vector2 vel = new Vector2(myRigidbody.velocity.x,0);
            // myRigidbody.velocity = vel;
            // tempvel = myRigidbody.velocity;
            //  myRigidbody.Sleep();

      //  }
        //  else
        // {
        // //    if (myRigidbody.IsSleeping())
        //  {
        //      myRigidbody.WakeUp();
        //       myRigidbody.velocity=tempvel;
        //
        //   }

        // }
    //}
    void Jump()
    {
        if (Input.GetButtonDown("Jump") && jumpnum > 0)
        {
            Vector2 vel = myRigidbody.velocity;
            vel.y = Jumpspeed;
            myRigidbody.velocity = vel;
            jumpnum--;
            isjump = true;
        }
    }
    void CheckGround()
    {
        isground = myfeet.IsTouchingLayers(LayerMask.GetMask("Ground"));
        if (isground == true)
        {
            jumpnum = maxjumpnum;
            isjump = false;
            animator.SetBool("jump", false);
        }
        else
        {
            isjump = true;
            animator.SetBool("jump", true);
        }
    }
    public void Update()
    {
        if (wingyong110 == true)
        {
            RepelTimesex = 0;
            stifftimeex = 0;
        }
        if (RepelTimesex > 0)
        {
            RepelTimesex--;
            transform.position = new Vector3((transform.position.x - RepelDir * Time.deltaTime * RepelDistance), transform.position.y, 0);

        }
        if (stifftimeex > 0)
        {
            stifftimeex -= Time.deltaTime;
        }
        else
        {
            freeze = 1;
            animator.SetBool("stiff", false);
            if (movable)
            {
                Jump();
                Flip();
                Move();
            }
        }
        //if (stateinfo.IsName("Attack") || stateinfo.IsName("Stiff")) return;
        CheckGround();
        isdie();
        //Attack();
        if (transform.position.y < -20f)
        {
            Killplayer();
        }

    }
}
